package easyway.trainapp.webframework.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 所有后台bean的基类
 * 
 * @author caiwei
 */
public class BasicBackingBean {
	public Map<String,Serializable> map=new HashMap<String,Serializable>();//查询集合
	public int index;//查询当前页数
	public int pagetotal;//查询总页数
	public int counttotal;//查询总记录数
	public List<SelectItem> pages=new ArrayList<SelectItem>();//查询下拉列表
	
	private String msg="";
	// 得到logger
	protected static final Log logger = LogFactory
			.getLog(BasicBackingBean.class);

	// 得到FacesContext
	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}
/*
	// 添加错误上下文消息
	public void sendMsg(String msgId) {
		FacesMessage message = MessageFactory.getMessage(getFacesContext(),
				msgId, "");
		System.out.println("message"+message);
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		getFacesContext().addMessage(null, message);
	}

	// 添加带参数的错误上下文消息
	public void sendMsg(String msgId, String str) {
		FacesMessage message = MessageFactory.getMessage(getFacesContext(),
				msgId, str);
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		getFacesContext().addMessage(null, message);
	}

	// 添加带两个参数的错误上下文消息
	public void sendMsg(String msgId, String str1, String str2) {
		FacesMessage message = MessageFactory.getMessage(getFacesContext(),
				msgId, str1, str2);
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		getFacesContext().addMessage(null, message);
	}
*/
	// 得到容器中相应的参数
	protected Object getAttribute(String pid) {
		return this.getExternalContext().getRequestParameterMap().get(pid);
	}
	

	// 得到HttpServletRequest
	protected HttpServletRequest getRequest() {
		return (HttpServletRequest) getExternalContext().getRequest();
	}

	// 得到ExternalContext
	public ExternalContext getExternalContext() {
		return getFacesContext().getExternalContext();
	}

	// 得到session
	public HttpSession getSession() {
		return (HttpSession) this.getExternalContext().getSession(true);
	}
	// 把值放进session里
	public void setSessionAttribute(String str,Object o){
		getExternalContext().getSessionMap().put(str, o);
	}
	// 从session中取值
	public Object getSessionAttribute(String str){
		return getExternalContext().getSessionMap().get(str);
	}
	// 得到资源文件中的消息
	public String getBundleString(String messageId) {
		return MessageFactory.getMessageString(getFacesContext(), messageId);
	}
	//获取后台bean实例
	public Object  getBackingBean(String beanstr,Class beanclass){
	ELContext elContext =getFacesContext().getELContext(); 
    ExpressionFactory expressionFactory = getFacesContext().getApplication().getExpressionFactory();        
    ValueExpression ve = expressionFactory.createValueExpression(elContext, beanstr, beanclass);
	return ve.getValue(elContext);
	}
	//更改操作信息方法
	public void updateOprateHint(String ostr){
		OprateHintBB oh=new OprateHintBB();
		oh=(OprateHintBB)getBackingBean("#{oprateHintBB}", OprateHintBB.class);
		oh.setOstr(ostr);
	}
	//getter and setter

	
	
	
	public Map<String, Serializable> getMap() {
		return map;
	}

	public void setMap(Map<String, Serializable> map) {
		this.map = map;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPagetotal() {
		return pagetotal;
	}

	public void setPagetotal(int pagetotal) {
		this.pagetotal = pagetotal;
	}

	public List<SelectItem> getPages() {
		pages=new ArrayList<SelectItem>();
		for(int i=1;i<=pagetotal;i++){
			pages.add(new SelectItem(""+i,"第"+i+"页"));
		}
		return pages;
	}

	public int getCounttotal() {
		return counttotal;
	}

	public void setCounttotal(int counttotal) {
		this.counttotal = counttotal;
	}

	public void setPages(List<SelectItem> pages) {
		this.pages = pages;
	}
	
	public void addMsg(String str)
	{	
		if(str!=null&&str!=""){
			StringBuffer sb = new StringBuffer();
			sb.append(str);
			msg=sb.toString();
		}else{
		msg=str; 
		}
	}

	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

//	public int[] getPages() {
//		 pages=new int[pagetotal];
//		 for(int i=0;i<pagetotal;i++){
//			 pages[i]=i;
//		 }
//		 
//		return pages;
//	}
//
//	public void setPages(int[] pages) {
//		this.pages = pages;
//	}
/**
 * 读取属性文件
 */
	public String loadPerporties(String propath,String pkey){
		String pvalue="";
		Properties pops = new Properties();
		  try {
			pops.load(BasicBackingBean.class.getClassLoader().getResourceAsStream(propath));
			pvalue=pops.getProperty(pkey);
		  } catch (Exception e) {
			logger.error("读取属性文件时错误",e);
		}
		return pvalue;
	}
	
	
	
} 
